package com.appengine.utils;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONException;
import org.json.JSONStringer;
 
import android.util.Log;

import com.datatypes.GeoPtr;
import com.datatypes.Track;
import com.google.gson.Gson;
 

// this class is for DEBUG only
public class LogUtils
{
	public static String SendLogURL = "http://sporteamtest.appspot.com/SendLog";

	// send msg to appengine log
	public static void SendMsgLog(String msg) 
	{
		SetPolicies();

		// prepare json
		JSONStringer json = null;
		try 
		{
			json = new JSONStringer().object().key("debug_code")
					.value("normal_msg").key("msg").value(msg)

					.endObject();
		} 
		catch (JSONException e) 
		{
			Log.e("json exeption", e.toString());
		}

		// send json
		JsonUtils.SendJsonStringer(json,SendLogURL);

	}

	public static void SendGeoPtrLog(GeoPtr point)
	{
		SetPolicies();
		JSONStringer json = null;

		// convert point to gson string
		Gson gson = new Gson();
		String PointStr = gson.toJson(point);

		//prepare Json
		try 
		{
			json = new JSONStringer().object().key("debug_code")
					.value("geoptr").key("msg").value(PointStr)

					.endObject();
		}
		catch (JSONException e) 
		{
			Log.e("json exeption", e.toString());
		}

		// send json
		JsonUtils.SendJsonStringer(json,SendLogURL);

	}
	
	public static void SendGeoPtrLstLog(List<GeoPtr> points)
	{
		SetPolicies();
		JSONStringer json = null;

		// convert point to gson string
		Gson gson = new Gson();
		String PointStr = gson.toJson((ArrayList<GeoPtr>)points);

		//prepare Json
		try 
		{
			json = new JSONStringer().object().key("debug_code")
					.value("geoptr_lst").key("msg").value(PointStr)

					.endObject();
		}
		catch (JSONException e) 
		{
			Log.e("json exeption", e.toString());
		}

		// send json
		JsonUtils.SendJsonStringer(json,SendLogURL);

	}
	
	public static void SendGeoPtrLstLog(ArrayList<GeoPtr> points)
	{
		SetPolicies();
		JSONStringer json = null;

		// convert point to gson string
		Gson gson = new Gson();
		String PointStr = gson.toJson(points);

		//prepare Json
		try 
		{
			json = new JSONStringer().object().key("debug_code")
					.value("geoptr_lst").key("msg").value(PointStr)

					.endObject();
		}
		catch (JSONException e) 
		{
			Log.e("json exeption", e.toString());
		}

		// send json
		JsonUtils.SendJsonStringer(json,SendLogURL);

	}
	
	
	public static void SendTrackLog(Track track)
	{
		SetPolicies();
		JSONStringer json = null;

		// convert point to gson string
		Gson gson = new Gson();
		String TrackStr = gson.toJson(track);

		//prepare Json
		try 
		{
			json = new JSONStringer().object().key("debug_code")
					.value("track").key("msg").value(TrackStr)

					.endObject();
		}
		catch (JSONException e) 
		{
			Log.e("json exeption", e.toString());
		}

		// send json
		JsonUtils.SendJsonStringer(json,SendLogURL);

	}
	
	

    //set policies for android 4 and up (for use of network IO from main UI thread)
	 public static void SetPolicies()
	 {
		 /* 
	        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
	        .detectDiskReads()
	        .detectDiskWrites()
	        .detectNetwork()   // or .detectAll() for all detectable problems
	        .penaltyLog()
	        .build());
	        
	         StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
	        .detectLeakedSqlLiteObjects()
	        .detectLeakedSqlLiteObjects()
	        .penaltyLog()
	        .penaltyDeath()
	        .build()); */
	 }


}
